package com.fancy.mobileschool.ui.adapt;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.fancy.mobileschool.MyConfiguration;
import com.fancy.mobileschool.R;
import com.fancy.mobileschool.entity.Attendance;
import com.fancy.mobileschool.entity.AttendanceResult;
import com.fancy.mobileschool.service.AttendService;
import com.fancy.mobileschool.ui.MessageDetailActivity;
import com.fancy.mobileschool.util.LocationHelper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Jackie on 2017/10/3.
 * self adapt for RecyclerView
 */


public class MessageAdapt extends RecyclerView.Adapter<MessageAdapt.MessageViewHolder> {

    private Context context;

    private List<Attendance> attendances;

    private AttendService attendService = new AttendService();

    private LocationHelper locationHelper;

    public MessageAdapt(Context context, List<Attendance> attendances){
        this.context = context;
        this.attendances = attendances;
        locationHelper = new LocationHelper(context);
    }

    @Override
    public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MessageViewHolder messageViewHolder = new MessageViewHolder(LayoutInflater.from(context).inflate(
                R.layout.fragment_message_item, parent, false), context);

        return messageViewHolder;
    }

    @Override
    public void onBindViewHolder(MessageViewHolder holder, int position) {
        if (position%2 == 0){
            holder.getIcon().setImageResource(R.drawable.time);
        }else
            holder.getIcon().setImageResource(R.drawable.icon);

        holder.getMessage().setText(attendances.get(position).course_name);
        if (MyConfiguration.type.equals("student"))
            holder.getRemark().setText("请点击进行签到");
        else
            holder.getRemark().setText("点击查看签到情况");

        holder.setAttendance(attendances.get(position));
    }

    @Override
    public int getItemCount() {
        return attendances.size();
    }

    class MessageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private ImageView icon;

        private TextView message;

        private TextView remark;

        private Attendance attendance;

        private Context context;

        public MessageViewHolder(View itemView, Context context){
            super(itemView);
            this.context = context;
            icon = (ImageView) itemView.findViewById(R.id.message_item_img);
            message = (TextView) itemView.findViewById(R.id.message_text);
            remark = (TextView) itemView.findViewById(R.id.message_item_time_text);
            setOnclick(itemView);
        }


        public ImageView getIcon() {
            return icon;
        }

        public TextView getMessage() {
            return message;
        }

        public void setIcon(ImageView icon) {
            this.icon = icon;
        }

        public void setMessage(TextView message) {
            this.message = message;
        }

        public TextView getRemark() {
            return remark;
        }

        private void setOnclick(View view){
            view.setOnClickListener(this);
        }

        public void setAttendance(Attendance attendance) {
            this.attendance = attendance;
        }

        @Override
        public void onClick(View view) {
            if (view.getId() == R.id.message_item){
                SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd");
                Date date = new Date();


                String dayStr = day.format(date);
                int status = 0;
                if (locationHelper.isEnter())
                    status = 1;
                else
                    status = 2;
                AttendanceResult result = new AttendanceResult(MyConfiguration.myID, attendance.course_id, dayStr, status);
                StudentAttend studentAttend = new StudentAttend();
                studentAttend.execute(result);

            }
        }

        class StudentAttend extends AsyncTask<AttendanceResult, Void, AttendanceResult> {

            @Override
            protected AttendanceResult doInBackground(AttendanceResult... params) {
                AttendanceResult attendanceResult = attendService.studentAttend(params);
                return attendanceResult;
            }

            @Override
            protected void onPostExecute(AttendanceResult attendanceResult) {
                if (attendanceResult != null)
                    Toast.makeText(context, "签到成功", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(context, "签到失败", Toast.LENGTH_SHORT).show();
            }
        }
    }

}


